﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using MemberShipDAL.DataObject;
using MemberServerBLL;

public partial class ControlPanel_Role_Privilege : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        #region 获得参数

        if (Request.QueryString.Count != 4)
        {
            Response.Write("<br/><br/><div align=\"center\"><font color=\"red\">请在左边树菜单中选择操作</font></div>");
            Response.End();
        }
        //权限组ID
        string GroupId = string.Empty;
        //角色ID
        string RoleId = string.Empty;

        //获得RoleName
        if (Request.QueryString["Name"] != null)
        {
            lbl_RoleName.Text = Request.QueryString["Name"];
        }
        //获得ID
        if (Request.QueryString["Id"] != null)
        {
            RoleId = Request.QueryString["Id"].ToString();
            lbl_GroupName.Attributes.Add("RoleId", RoleId);
        }

        if (Request.QueryString["Id"] != null)
        {
            RoleId = Request.QueryString["Id"].ToString();
            lbl_GroupName.Attributes.Add("RoleId", RoleId);
        }
        //获取权限组
        if (Request.QueryString["Gid"] != null)
        {
            GroupId = Request.QueryString["Gid"].ToString();
            lbl_GroupName.Attributes.Add("Gid", GroupId);
        }
        //获取权限组名称
        if (Request.QueryString["GName"] != null)
        {
            lbl_GroupName.Text = HttpUtility.UrlDecode(Request.QueryString["GName"].ToString());
        }
        #endregion

        if (!IsPostBack)
        {
            BindPrivilege();
        }
    }

    /// <summary>
    /// 创建行的时候进行处理
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gv_Privilege_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView drv = (DataRowView)e.Row.DataItem;
            if (drv == null)
            {
                return;
            }
            for (int i = 0; i < drv.DataView.Table.Columns.Count; i++)
            {
                if (drv.DataView.Table.Columns[i].ColumnName != "资源名称")
                {
                    CheckBox chk = (CheckBox)e.Row.Cells[i].Controls[0];

                    if (Convert.ToBoolean(drv[drv.DataView.Table.Columns[i].ColumnName].ToString()))
                    {
                        //在此该读取PrivileveInRoles表里面的内容来判断界面上的CheckBox是否选择
                        string roleId = lbl_GroupName.Attributes["RoleId"].ToString();
                        string privilegeId = new BLL_Privilege().GetPrivilegeId(drv[0].ToString(), drv.DataView.Table.Columns[i].ColumnName);
                        drv[i] = BLL_PrivilegeInRoles.PrivilegeInRolesIsExists(new Guid(roleId), new Guid(privilegeId));
                        chk.Enabled = true;
                    }
                    else
                    {
                        chk.Visible = false;
                    }
                }

            }
        }

    }


    protected void btn_Save_Click(object sender, EventArgs e)
    {
        try
        {
            //把勾选的数据保存起来
            foreach (GridViewRow row in gv_Privilege.Rows)
            {
                foreach (TableCell cell in row.Cells)
                {
                    //如果单元格包含Cell控件
                    if (cell.Controls.Count == 1)
                    {
                        //找出控件
                        CheckBox chk = (CheckBox)cell.Controls[0];
                        string privilegeId = new BLL_Privilege().GetPrivilegeId(row.Cells[0].Text, chk.ToolTip);
                        if (privilegeId != "")
                        {
                            Guid privilegeGuid = new Guid(privilegeId);
                            Guid roleGuid = new Guid(lbl_GroupName.Attributes["RoleId"].ToString());

                            //更新表里面的信息
                            new BLL_PrivilegeInRoles().UpdatePrivilegeInRoles(roleGuid, privilegeGuid, chk.Checked);
                        }
                    }
                }
            }
            lbl_SaveResult.Text = "保存成功";
        }
        catch (Exception ex)
        {
            lbl_SaveResult.Text = "保存失败，错误信息：" + ex.Message;
        }
        finally
        {
            BindPrivilege();
        }


    }

    /// <summary>
    /// //重新数据绑定
    /// </summary>
    private void BindPrivilege()
    {
        gv_Privilege.DataSource = BLL_Privilege.PrivilegeDataBind(lbl_GroupName.Attributes["Gid"].ToString());
        gv_Privilege.RowHeaderColumn = "资源名称";
        gv_Privilege.DataBind();
    }



}
